import json

import requests
import os
import queue
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED, ALL_COMPLETED


def func():
    while True:
        
        global qu_befor
        if qu_befor.empty():
            return -1
        i = qu_befor.get()
        print("{}剩下{}".format(i, qu_befor.qsize()))
        proxy = {
            'http': 'http://{}'.format(i),
            'https': 'https://{}'.format(i),
        }
        '''head 信息'''
        head = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
            'Connection': 'keep-alive'}
        try:
            p = requests.get('http://icanhazip.com', headers=head, proxies=proxy, timeout=2.5)
            if len(p.text) < 15:
                # print(p.text)
                if qu.full():
                    return 0
                qu.put(i)
                continue
        except requests.exceptions.ReadTimeout:
            continue
        except requests.exceptions.ConnectTimeout:
            continue
        except requests.exceptions.ProxyError:
            continue
        except ValueError:
            continue
        continue
    
    return 1

qu_befor = queue.Queue(maxsize=3000)
qu = queue.Queue(maxsize=200)

for i in open("./proxy"):
    # 代理IP放入队列
    qu_befor.put(i)

with ThreadPoolExecutor(max_workers=400) as t:
    all_task = [t.submit(func) for i in range(1, 400)]
    wait(all_task, return_when=ALL_COMPLETED)
    print("finished")
f = open("./proxy.log", mode="w+")
# print(qu.qsize())
s = requests.session()
ts = []
while True:
    tmp = qu.get()
    f.write(tmp)
    ts.append(tmp.replace("\n",""))
    # print(ts,tmp)
    if qu.empty():
        break
f.close()
s.headers.update({"X-CSRFToken":s.get("http://127.0.0.1:8088/gettoken").json().get("token",None)})
print(ts)
print(s.post(url="http://127.0.0.1:8088/setip/",data={'proxy':ts}).text)
s.close()